package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//@CrossOrigin
@RequestMapping("/admin/product")
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    // 查询一级分类
    @GetMapping("/getCategory1")
    public Result getCategory1() {

        List<BaseCategory1> bc1List = managerService.getCategory1();
        return Result.ok(bc1List);
    }

    // 查询二级分类
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable(value = "category1Id") Long category1Id) {

        List<BaseCategory2> bc2List = managerService.getCategory2(category1Id);
        return Result.ok(bc2List);
    }

    // 查询三级分类
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable(value = "category2Id") Long category2Id) {

        List<BaseCategory3> bc3List = managerService.getCategory3(category2Id);
        return Result.ok(bc3List);
    }

    // 根据分类id获取属性列表
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getAttrInfoList(
            @PathVariable(value = "category1Id") Long category1Id,
            @PathVariable(value = "category2Id") Long category2Id,
            @PathVariable(value = "category3Id") Long category3Id
    ) {

        List<BaseAttrInfo> baInfo = managerService.getAttrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(baInfo);
    }

    // 添加平台属性
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {

        managerService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    // 根据属性id获取属性值列表
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable(value = "attrId") Long attrId) {

        BaseAttrInfo baseAttrInfo = managerService.getAttrValueList(attrId);
        List<BaseAttrValue> baseAttrValueList = baseAttrInfo.getAttrValueList();
        return Result.ok(baseAttrValueList);
    }

    // 查找品牌 分页
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result baseTrademark(
            @PathVariable(value = "page") Integer page,
            @PathVariable(value = "limit") Integer limit) {

        IPage iPage = managerService.baseTrademark(page, limit);
        return Result.ok(iPage);
    }

    // 查询品牌集合
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList() {

        List<BaseTrademark> baseTrademarkList = managerService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    // 获取销售属性
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList() {

        List<BaseSaleAttr> baseSaleAttrs = managerService.baseSaleAttrList();
        return Result.ok(baseSaleAttrs);
    }

    // 添加spu
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {

        managerService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    // 查询分页结果 SPU商品信息
    @GetMapping("/{page}/{limit}")
    public Result spuInfoPage(
            @ApiParam(value = "分页数", required = true)
            @PathVariable(name = "page") Long page,
            @ApiParam(value = "每页最大条数", required = true)
            @PathVariable(name = "limit") Long limit,
            @ApiParam(value = "三级分类id", required = true) Long category3Id
    ) {

        IPage<SpuInfo> spuInfoIPage = managerService.spuInfoPage(page, limit, category3Id);
        return Result.ok(spuInfoIPage);
    }

    // 根据spuId查询所有图片
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable(value = "spuId") Long spuid) {

        List<SpuImage> spuImageList = managerService.spuImageList(spuid);
        return Result.ok(spuImageList);
    }

    // 根据spuId获取销售属性
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable("spuId") Long spuId) {

        List<SpuSaleAttr> spuSaleAttrList = managerService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    // 添加sku
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {

        // sku信息表
        // 图片表 集合
        // 平台属性表 集合
        // 销售属性表 集合
        managerService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    // 获取sku分页列表
    @GetMapping("/list/{page}/{limit}")
    public Result list(
            @PathVariable("page") Long page,
            @PathVariable("limit") Long limit) {

        IPage<SkuInfo> skuInfoIPage = managerService.skuList(page, limit);
        return Result.ok(skuInfoIPage);
    }

    // 上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId) {

        managerService.onSale(skuId);
        return Result.ok();
    }

    // 下架
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId) {

        managerService.cancelSale(skuId);
        return Result.ok();
    }
}
